<?php
class EarnedReportByStateForm extends CFormModel{
	public $startDay;
	public $startYear;
	public $endDay;
	public $endYear;
    protected $_data;
    protected $_filename;

    public function rules(){
    	return array(
    		array('startDay,startYear','required'),
    	);
    }
    public function attributeLabels()
    {
        return array(
            'startDay'=>'Start Date',
            'startYear'=>'Start Year'
        );
    }

    public function getDayList(){
    	$days = array();
    	for($i=1;$i<=31;$i++){
    		$days[$i] = $i;
    	}
    	return $days;
    }

    public function getYearList(){
    	$years = array(
    		2011=>'2011',
    		2012=>'2012',
    		2013=>'2013',
    		2014=>'2014',
    		2015=>'2015'
    	);
    	return $years;
    }

    public function collectData(){
    	if($this->_data == null){
    		$cmd = Yii::app()->db->createCommand();
    		$cmd->select = '*';
	    	$cmd->from = 'tbl_contract_earned';
	    	$cmd->where = 'month=:month and year=:year';
	    	$cmd->order('year, month, state, county');	
	    	$cmd->bindValues(array(
	    		'month'=>$this->startDay,
	    		'year'=>$this->startYear
	    	));
	    	$this->_data = $cmd->queryAll();
	    	$this->generateFilename();
    	}
    	return $this->_data;
    }

    protected function generateFilename(){
    	if($this->_filename == null)
    		$this->_filename =  'csv/EarnedUnEarned_Start'.$this->startDay.'-'.$this->startYear.'_Date'.date('Y-m-d').'.csv';
    	return $this->_filename;
    }
    public function getCsv(){
    	$data = $this->collectData();    	
    	$string = 'Id,Contract,Month,Year,Term Months,Months Earned,Amountcharged,Billed,Billed Tax,Current Earned,Current Earned Tax,Sub-Total Earned,Sub-Total Earned Tax,Total Earned,County,State,Contract Type,Create_date,Bill Type,Months Remaining,Remaining,Remaining Tax'.PHP_EOL;
    	foreach($data as $row){
    		$string.=implode(',',$row).PHP_EOL;
    	}
    	
    	$f = fopen($this->_filename,'w+');
    	if($f!==false){
    		fwrite($f,$string);
    	}
    	fclose($f);    
    	Yii::app()->fileHelper->forceDownload($this->_filename);
    }

    public function prepareDataForHtml()
    {
    	$data= $this->collectData();
    	$result = array(
    		'earned'=>0,
    		'earnedTax'=>0,
    		'remainingTax'=>0,
    		'remaining'=>0
    	);

    	foreach($data as $row)
    	{
    		$result['earned']+=$row['currentEarned'];
    		$result['earnedTax']+=$row['currentEarnedTax'];
    		$result['remaining']+=$row['remaining'];
    		$result['remainingTax']+=$row['remainingTax'];
    		if( ! isset($result['items'][$row['state']]))
    		{
				$result['items'][$row['state']]['earned'] = $row['currentEarned'];
    			$result['items'][$row['state']]['earnedTax'] = $row['currentEarnedTax'];
    			$result['items'][$row['state']]['remaining'] = $row['remaining'];
    			$result['items'][$row['state']]['remainingTax'] = $row['remainingTax'];
    		}
    		else
    		{
    			$result['items'][$row['state']]['earned'] += $row['currentEarned'];
    			$result['items'][$row['state']]['earnedTax'] += $row['currentEarnedTax'];
    			$result['items'][$row['state']]['remaining'] += $row['remaining'];
    			$result['items'][$row['state']]['remainingTax'] += $row['remainingTax'];
    		}

    		if( ! isset($result['items'][$row['state']]['items'][$row['county']]))
    		{
    			$result['items'][$row['state']]['items'][$row['county']]['earned'] = $row['currentEarned'];
    			$result['items'][$row['state']]['items'][$row['county']]['earnedTax'] = $row['currentEarnedTax'];
    			$result['items'][$row['state']]['items'][$row['county']]['remaining'] = $row['remaining'];
    			$result['items'][$row['state']]['items'][$row['county']]['remainingTax'] = $row['remainingTax'];
    		}
    		else
    		{
    			$result['items'][$row['state']]['items'][$row['county']]['earned'] += $row['currentEarned'];
    			$result['items'][$row['state']]['items'][$row['county']]['earnedTax'] += $row['currentEarnedTax'];
    			$result['items'][$row['state']]['items'][$row['county']]['remaining'] += $row['remaining'];
    			$result['items'][$row['state']]['items'][$row['county']]['remainingTax'] += $row['remainingTax'];
    		}
    	}
    	return $result;
    }

}